package com.example.classmanageapp;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.classmanageapp.Data.ItemEntity;
import com.example.classmanageapp.adapter.ListAdapter;
import com.example.classmanageapp.sqlite.MyDatabaseHelper;
import com.example.classmanageapp.utils.SysUtils;
import com.example.classmanageapp.view.PullUpListView;

import java.util.ArrayList;

public class MessageTeacherActivity extends Activity {

    private SwipeRefreshLayout refreshLayout;
    private PullUpListView listView;
    private ArrayList<ItemEntity> lists;
    private ListAdapter adapter;
    private Integer page;
    private MyDatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SysUtils.setStatusBarColor(this, true, 0);
        SysUtils.setSkin(this);
        setContentView(R.layout.activity_teacher_message);

        ImageButton img_teacher_back = findViewById(R.id.img_teacher_back);
        img_teacher_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        refreshLayout = findViewById(R.id.swipeRe_fresh);
        listView = findViewById(R.id.list_view);
        lists = new ArrayList<>();
        page = 1;
        getMsg();
        refreshLayout.setOnRefreshListener(() -> refreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                page ++;
                getMsg();
                //Toast.makeText(getActivity(), "sdfsdfsdf", Toast.LENGTH_SHORT).show();
            }
        }, 3000));


        listView.setPullUpListViewCallBack(() -> listView.postDelayed(new Runnable() {
            @Override
            public void run() {
                ItemEntity itemEntity = new ItemEntity();
                itemEntity.setTitle("模拟数据");
                itemEntity.setContent("魔法师规范的好几个号防堵膏糊味儿后顾热火顾不符合该部分的好不烦得很不服输的环保股单方事故");
                itemEntity.setUrl("https://img.ivsky.com/img/tupian/co/202009/13/yellowstone_national_park-005.jpg");
                lists.add(itemEntity);
                adapter.mData = lists;
                adapter.notifyDataSetChanged();
                listView.completeRefresh();
                //Toast.makeText(getActivity(), "sdfdsfsdfds", Toast.LENGTH_SHORT).show();
            }
        }, 2000));

        listView.setOnItemClickListener((adapterView, view, i, l) -> {
            ItemEntity item = adapter.getItem(i);
            Toast.makeText(this, "click the " + i + l + " sfgd", Toast.LENGTH_SHORT).show();
        });

        TextView tv_teacher_edit = findViewById(R.id.tv_teacher_edit);
        tv_teacher_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MessageTeacherActivity.this,
                        MessageEditActivity.class);
                startActivity(intent);
            }
        });
    }

    //获取通知内容
    private void getMsg() {
        //String sql = "SELECT * FROM tb_msg where 1 = 1 LIMIT 5 OFFSET 0";

        dbHelper = new MyDatabaseHelper(this, "COURSE_SCHEDULE.db", null, 1);


        try {
            //新增消息内容
            SQLiteDatabase db = dbHelper.getReadableDatabase();
            String sql = "SELECT * FROM tb_msg where 1 = 1 ";
            Cursor cursor = db.rawQuery(sql, null);
            if (cursor.moveToFirst()) {

                lists.clear();
                ItemEntity itemEntity = new ItemEntity();

                //通过列名“time”获取该列索引，再根据索引获取对应的数据。
                itemEntity.setTitle(cursor.getString(cursor.getColumnIndex("msgtitle")));
                itemEntity.setContent(cursor.getString(cursor.getColumnIndex("msgcontent")));
                itemEntity.setUrl("https://img.ivsky.com/img/tupian/co/202009/13/yellowstone_national_park-005.jpg");
                lists.add(itemEntity);
                while(cursor.moveToNext()){
                    ItemEntity tmp = new ItemEntity();

                    //通过列名“time”获取该列索引，再根据索引获取对应的数据。
                    tmp.setTitle(cursor.getString(cursor.getColumnIndex("msgtitle")));
                    tmp.setContent(cursor.getString(cursor.getColumnIndex("msgcontent")));
                    tmp.setUrl("https://img.ivsky.com/img/tupian/co/202009/13/yellowstone_national_park-005.jpg");
                    lists.add(tmp);
                }
            } else {
                   Toast.makeText(MessageTeacherActivity.this, "暂无数据", Toast.LENGTH_SHORT).show();
            }
            cursor.close();

            if (page == 1) {
                adapter = new ListAdapter(this, lists);

                listView.setAdapter(adapter);
                listView.initBottomView();
            }else {
                adapter.mData = lists;
                adapter.notifyDataSetChanged();
                refreshLayout.setRefreshing(false);
            }
        }catch (Exception e) {
            Toast.makeText(this, "数据解析失败，请刷新重试!!", Toast.LENGTH_SHORT).show();
        }
    }

}
